// runcompressorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "runcompressorDlg.h"
#include "waveeditor.h"



// CruncompressorDlg dialog


RunCompressorDlg::RunCompressorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(RunCompressorDlg::IDD, pParent)
	, htotalnumber(0)
	, vtotalnumber(0)
	, rootdirectory(_T(""))
{
}

RunCompressorDlg::~RunCompressorDlg()
{
}

void RunCompressorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control		(pDX, IDC_ADDTEXTTOTAIL	, iftexttotailchecked);
	DDX_Control		(pDX, IDC_TEXTTOTAIL	, texttotail);
	DDX_Control		(pDX, IDC_OUTPUTFILE	, ifoutputcheck);
	DDX_Control		(pDX, IDC_OUTPUTNAME	, output_linectrl);
	DDX_Control		(pDX, IDC_BROWSEOUTPUT	, browseoutputctr);
	DDX_Control		(pDX, IDC_CHECKHTOTAL	, htotalctrl);
	DDX_Control		(pDX, IDC_HTOTAL		, htotalnumberctrl);
	DDX_Control		(pDX, IDC_CHECKVTOTAL	, vtotalctrl);
	DDX_Control		(pDX, IDC_VTOTAL		, vtotalnumberctrl);
	DDX_Text		(pDX, IDC_HTOTAL		, htotalnumber);
	DDV_MinMaxInt	(pDX					, htotalnumber, 0, 10000);
	DDX_Text		(pDX, IDC_VTOTAL		, vtotalnumber);
	DDV_MinMaxInt	(pDX					, vtotalnumber, 0, 10000);
	DDX_Control		(pDX, IDC_EE			, eechecked);
	DDX_Control		(pDX, IDC_EENUMBERS		, eectrl);
	DDX_Control		(pDX, IDC_INPUTFILES	, inputfilesctrl);
	DDX_Control		(pDX, IDC_WAVEEDITOR	, waveeditorcheck);
	DDX_Control		(pDX, IDC_STAT			, statctrl);
	eectrl.SetCurSel(1);
	DDX_Control		(pDX, IDC_COMPRESSORSTEP, compressorstepctrl);
	compressorstepctrl.SetCurSel(1);
	DDX_Control		(pDX, IDC_TWOPASS		, twopassctrl);
	DDX_Control		(pDX, IDC_LOADREQCHECK	, loadreccheck);
	DDX_Control		(pDX, IDC_LOADREQ		, loadrecctrl);
	DDX_Text		(pDX, IDC_LOADREQ		, loadrec);
	DDV_MinMaxInt	(pDX					, loadrec, 0, 10000);
	DDX_Control		(pDX, IDC_FINERESCHECK	, finerescheck);
	DDX_Control		(pDX, IDC_FINERES		, fineresctrl);
	DDX_Text		(pDX, IDC_FINERES		, fineres);
	DDV_MinMaxInt	(pDX					, fineres, 0, 10000);
	DDX_Control		(pDX, IDC_RESETSUBCHECK	, resetsubcheck);
	DDX_Control		(pDX, IDC_RESETSUB		, resetsubctrl);
	DDX_Text		(pDX, IDC_RESETSUB		, resetsub);
	DDV_MinMaxInt	(pDX					, resetsub, 0, 10000);
}

BOOL RunCompressorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(RunCompressorDlg	, CDialog)
	ON_BN_CLICKED(IDC_EE			, OnBnClickedEe)
	ON_BN_CLICKED(IDC_COMPRESS		, OnBnClickedCompress)
	ON_BN_CLICKED(IDC_BROWSEINPUT	, OnBnClickedBrowseinput)
	ON_BN_CLICKED(IDC_OUTPUTFILE	, OnBnClickedOutputfile)
	ON_BN_CLICKED(IDC_BROWSEOUTPUT	, OnBnClickedBrowseoutput)
	ON_BN_CLICKED(IDC_CHECKHTOTAL	, OnBnClickedCheckhtotal)
	ON_BN_CLICKED(IDC_CHECKVTOTAL, OnBnClickedCheckvtotal)
	ON_BN_CLICKED(IDC_LOADREQCHECK, OnBnClickedLoadreqcheck)
	ON_BN_CLICKED(IDC_FINERESCHECK, OnBnClickedFinerescheck)
	ON_BN_CLICKED(IDC_RESETSUBCHECK, OnBnClickedResetsubcheck)
	ON_CBN_SELCHANGE(IDC_COMPRESSORSTEP, OnCbnSelchangeCompressorstep)
	ON_BN_CLICKED(IDC_ADDTEXTTOTAIL, OnBnClickedAddtexttotail)
	ON_BN_CLICKED(IDC_BUILDSHOWCOMMANDLINE, OnBnClickedBuildshowcommandline)
	ON_BN_CLICKED(IDC_SHOWLOGAFTERCOMPRESS, OnBnClickedShowlogaftercompress)
END_MESSAGE_MAP()




// CruncompressorDlg message handlers


void RunCompressorDlg::OnBnClickedOutputfile()
{
	// TODO: Add your control notification handler code here
	if(ifoutputcheck.GetCheck()	==	BST_CHECKED)
	{
		output_linectrl.EnableWindow();
		browseoutputctr.EnableWindow();
	}
	else
	{
		output_linectrl.EnableWindow(FALSE);
		browseoutputctr.EnableWindow(FALSE);
	}
	
}


void RunCompressorDlg::OnBnClickedCheckhtotal()
{
	// TODO: Add your control notification handler code here
	if(htotalctrl.GetCheck()	==	BST_CHECKED)
	{
		htotalnumberctrl.EnableWindow();
	}
	else
	{
		htotalnumberctrl.EnableWindow(FALSE);
	}
}


void RunCompressorDlg::OnBnClickedCheckvtotal()
{
	// TODO: Add your control notification handler code here
	if(vtotalctrl.GetCheck()	==	BST_CHECKED)
	{
		vtotalnumberctrl.EnableWindow();
	}
	else
	{
		vtotalnumberctrl.EnableWindow(FALSE);
	}
}



void RunCompressorDlg::OnBnClickedEe()
{
	// TODO: Add your control notification handler code here
	if(eechecked.GetCheck()	==	BST_CHECKED)
	{
		eectrl.EnableWindow();
	}
	else
	{
		eectrl.EnableWindow(FALSE);
	}
}



void RunCompressorDlg::OnBnClickedBrowseoutput()
{
	// TODO: Add your control notification handler code here
	CString sline;
	char Filters[]= "Bin Files (*.bin)|*.bin|All Files (*.*)|*.*||";
	output_linectrl.GetLine(0,sline.GetBuffer(1000),1000);
	CFileDialog file(FALSE,"bin",sline,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, Filters);
	if (file.DoModal() == IDCANCEL)	
	{
		return ;
	}
	output_linectrl.SetWindowText(file.GetPathName());
}



void RunCompressorDlg::OnBnClickedBrowseinput()
{
	// TODO: Add your control notification handler code here
	CString sline;
	char Filters[]= "Vector Files (*.txt)|*.txt|All Files (*.*)|*.*||";
	CFileDialog file(TRUE,"txt",sline,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT , Filters);
	if (file.DoModal() == IDCANCEL)	
	{
		return ;
	}
	
	POSITION pos	=file.GetStartPosition();
	if(pos == NULL)
	{
		return;
	}
	while(pos != NULL)
	{
		CString  name	='"'+file.GetNextPathName(pos)+'"';
		inputfilesctrl.ReplaceSel(" "+name);
	}
}



int RunCompressorDlg::RunCompressor(CString commandline)
{
	FILE *batFile ;
	ZWaveeditorApp	*pWinapp	=	(ZWaveeditorApp*) AfxGetApp();
	rootdirectory				=	pWinapp->OutputDirectory;
	SetCurrentDirectory(rootdirectory);
	batFile	=	fopen("runcompressor.bat","w");
	if(batFile	!=	NULL)
	{
		fwrite(LPCTSTR(commandline),commandline.GetLength(),1,batFile);
		fclose(batFile);
		do{		
			batFile	=	fopen("runcompressor.bat","r");
		}while(batFile	== NULL);
		fclose(batFile);
		ShellExecute(NULL,NULL,"runcompressor.bat",NULL,NULL,SW_HIDE);
	}
	else
	{
		AfxMessageBox("Incorrect directory - check ini file");
	}
	return 0;
}

void RunCompressorDlg::OnBnClickedCompress()
{
	// TODO: Add your control notification handler code here
	ZWaveeditorApp	*pWinapp	=	(ZWaveeditorApp*) AfxGetApp();
	CString			directory	=	'"'+pWinapp->ConfigurationDirectory+"\\";
	CString			commandname = directory+"utg_compressor.exe "+'"';
	char param[1000];
	if(waveeditorcheck.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -waveeditor";
	}

	if(eechecked.GetCheck()	==	BST_CHECKED)
	{
		int index	=	eectrl.GetCurSel();
		eectrl.GetLBText(index,param);
		commandname +=" -ee "+CString(param);
	}
	
	if(twopassctrl.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -two_pass ";
	}
	
	if(htotalctrl.GetCheck()	==	BST_CHECKED)
	{
		htotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -htotal "+CString(param);
		}
	}
	
	if(vtotalctrl.GetCheck()	==	BST_CHECKED)
	{
		vtotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -vtotal "+CString(param);
		}
	}
//-----------------  Compressor Step Configurations --------------------------------------------------
	int index = compressorstepctrl.GetCurSel();
	if(index == 1)
	{
		commandname	+=	" -stepB ";
		if(loadreccheck	.GetCheck()	==	BST_CHECKED)
		{
			loadrecctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -load_req "+CString(param);
			}
		}
		if(finerescheck	.GetCheck()	==	BST_CHECKED)
		{
			fineresctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -fine_res "+CString(param);
			}
		}
		if(resetsubcheck	.GetCheck()	==	BST_CHECKED)
		{
			resetsubctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -reset_sub "+CString(param);
			}
		}
	}
	if(index == 0)
	{
		commandname	+=	" -stepA ";
	}

	
//--------------------------------------------------------------------------------------
	if(ifoutputcheck.GetCheck()	==	BST_CHECKED)
	{
		output_linectrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname += " -output "+'"'+CString(param)+'"';
		}
	}
	

//-----------------------------------------------------------------------------------------------------
	inputfilesctrl.GetLine(0,param,1000);
	if(strlen(param) > 1)
	{
		commandname +=" -input "+CString(param);
	}
	else
	{
		AfxMessageBox("Error no input files",MB_OK|MB_ICONSTOP);
		return;
	}
//-----------------------------------------------------------------------------------------------------
	if(statctrl.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -stat ";
	}
	else
	{
//		commandname +=" -";
	}

//--------------------------------------------------------------------------------------
	if(iftexttotailchecked.GetCheck()	==	BST_CHECKED)
	{
		texttotail.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname += '"'+CString(param)+'"';
		}
	}
	

	commandname += " >utg_compressor.log";
	RunCompressor(commandname);
}



void RunCompressorDlg::OnBnClickedLoadreqcheck()
{
	// TODO: Add your control notification handler code here
	if(loadreccheck.GetCheck()	==	BST_CHECKED)
	{
		loadrecctrl.EnableWindow();
	}
	else
	{
		loadrecctrl.EnableWindow(FALSE);
	}
}

void RunCompressorDlg::OnBnClickedFinerescheck()
{
	// TODO: Add your control notification handler code here
	if(finerescheck.GetCheck()	==	BST_CHECKED)
	{
		fineresctrl.EnableWindow();
	}
	else
	{
		fineresctrl.EnableWindow(FALSE);
	}
}

void RunCompressorDlg::OnBnClickedResetsubcheck()
{
	// TODO: Add your control notification handler code here
	if(resetsubcheck.GetCheck()	==	BST_CHECKED)
	{
		resetsubctrl.EnableWindow();
	}
	else
	{
		resetsubctrl.EnableWindow(FALSE);
	}
}

void RunCompressorDlg::OnCbnSelchangeCompressorstep()
{
	// TODO: Add your control notification handler code here
	int index = compressorstepctrl.GetCurSel();
	if(index == 0)
	{
		loadreccheck	.SetCheck(0);
		loadreccheck	.EnableWindow(FALSE);
		loadrecctrl		.EnableWindow(FALSE);
		finerescheck	.SetCheck(0);
		finerescheck	.EnableWindow(FALSE);
		fineresctrl		.EnableWindow(FALSE);
		resetsubcheck	.SetCheck(0);
		resetsubcheck	.EnableWindow(FALSE);
		resetsubctrl	.EnableWindow(FALSE);
		return;
	}
	if(index == 1)
	{
		loadreccheck	.EnableWindow();
		finerescheck	.EnableWindow();
		resetsubcheck	.EnableWindow();
		return;
	}
}

void RunCompressorDlg::OnBnClickedAddtexttotail()
{
	// TODO: Add your control notification handler code here
	if(iftexttotailchecked.GetCheck()	==	BST_CHECKED)
	{
		texttotail.EnableWindow(TRUE);
	}
	else
	{
		texttotail.EnableWindow(FALSE);
	}

}

void RunCompressorDlg::OnBnClickedBuildshowcommandline()
{
	// TODO: Add your control notification handler code here
	ZWaveeditorApp	*pWinapp	=	(ZWaveeditorApp*) AfxGetApp();
	CString			directory	=	'"'+pWinapp->ConfigurationDirectory+"\\";
	rootdirectory				=	pWinapp->OutputDirectory;
	SetCurrentDirectory(rootdirectory);
	CString			commandname = directory+"utg_compressor.exe "+'"';
	char param[1000];
	if(waveeditorcheck.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -waveeditor";
	}

	if(eechecked.GetCheck()	==	BST_CHECKED)
	{
		int index	=	eectrl.GetCurSel();
		eectrl.GetLBText(index,param);
		commandname +=" -ee "+CString(param);
	}
	
	if(twopassctrl.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -two_pass ";
	}
	
	if(htotalctrl.GetCheck()	==	BST_CHECKED)
	{
		htotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -htotal "+CString(param);
		}
	}
	
	if(vtotalctrl.GetCheck()	==	BST_CHECKED)
	{
		vtotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -vtotal "+CString(param);
		}
	}
//-----------------  Compressor Step Configurations --------------------------------------------------
	int index = compressorstepctrl.GetCurSel();
	if(index == 1)
	{
		commandname	+=	" -stepB ";
		if(loadreccheck	.GetCheck()	==	BST_CHECKED)
		{
			loadrecctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -load_req "+CString(param);
			}
		}
		if(finerescheck	.GetCheck()	==	BST_CHECKED)
		{
			fineresctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -fine_res "+CString(param);
			}
		}
		if(resetsubcheck	.GetCheck()	==	BST_CHECKED)
		{
			resetsubctrl.GetLine(0,param,1000);
			if(strlen(param) > 1)
			{
				commandname +=" -reset_sub "+CString(param);
			}
		}
	}
	if(index == 0)
	{
		commandname	+=	" -stepA ";
	}

	
//--------------------------------------------------------------------------------------
	if(ifoutputcheck.GetCheck()	==	BST_CHECKED)
	{
		output_linectrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname += " -output "+'"'+CString(param)+'"';
		}
	}
	

//-----------------------------------------------------------------------------------------------------
	inputfilesctrl.GetLine(0,param,1000);
	if(strlen(param) > 1)
	{
		commandname +=" -input "+CString(param);
	}
	else
	{
		AfxMessageBox("Error no input files",MB_OK|MB_ICONSTOP);
		return;
	}
//-----------------------------------------------------------------------------------------------------
	if(statctrl.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -stat ";
	}
	else
	{
//		commandname +=" -";
	}

//--------------------------------------------------------------------------------------
	if(iftexttotailchecked.GetCheck()	==	BST_CHECKED)
	{
		texttotail.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname += '"'+CString(param)+'"';
		}
	}
	
	commandname += " >utg_compressor.log";
	FILE *lastcommand	=	fopen("lastcommand.log","w");
	if(lastcommand	!=	NULL)
	{
		fwrite(LPCTSTR(commandname),commandname.GetLength(),1,lastcommand);
		fclose(lastcommand);
		ShellExecute(this->m_hWnd,"open","notepad","lastcommand.log",NULL,SW_NORMAL);
	}
}

void RunCompressorDlg::OnBnClickedShowlogaftercompress()
{
	// TODO: Add your control notification handler code here
	ZWaveeditorApp	*pWinapp	=	(ZWaveeditorApp*) AfxGetApp();
	CString			directory	=	'"'+pWinapp->ConfigurationDirectory+"\\";
	rootdirectory				=	pWinapp->OutputDirectory;
	SetCurrentDirectory(rootdirectory);
	ShellExecute(this->m_hWnd,"open","notepad","utg_compressor.log",NULL,SW_NORMAL);
}
